//
//  ChooseCurrencyViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/1/8.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import Kingfisher

class ChooseCurrencyViewController: QMUICommonViewController {
    
    var selectBlock:((MoneyItem)->())?
    
    var cancelBlock:(()->())?
    

    
    
    @IBOutlet weak var titleLabel: UILabel!
    

    
    
    @IBAction func closeClick(_ sender: UIButton) {
        if cancelBlock != nil {
            cancelBlock!()
        }
    }
   
    var isChoose:Bool = true
    var moneys = [String]()
   

    @IBOutlet weak var closeButton: UIButton!
    
    @IBOutlet weak var customTitleView: UIView!

    
    @IBOutlet weak var tableView: QMUITableView!


    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
        // Do any additional setup after loading the view.
    }
    
    func setupView() {
        self.tableView.register(UINib(nibName: "CurrencyViewCell", bundle: nil), forCellReuseIdentifier: "cellItem")
        self.tableView.separatorStyle = .none
        self.tableView.rowHeight = 64
        
        let key = "chooseCurrency"
        self.titleLabel.text = LanguageHelper.getString(key: key)
        
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}
extension ChooseCurrencyViewController:QMUITableViewDelegate,QMUITableViewDataSource,QMUIModalPresentationContentViewControllerProtocol {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return appMoneyItems.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellItem") as! CurrencyViewCell
        let item:MoneyItem = appMoneyItems[indexPath.row]
        cell.icon.kf.setImage(with: URL(string: IMAGE_HOST + item.image))
        cell.currency.text = item.ex_title
        
        let locale = LanguageHelper.shareInstance.locale
        if locale == "en" {
             cell.label.text = item.etitle
        } else if locale == "th" {
             cell.label.text = item.ttitle
        }else {
            cell.label.text = item.title
        }
       
        if moneys.count > 0 {
            if let index =  moneys.lastIndex(of: item.ex_title) {
                cell.hasIn.text = "\(index + 1)"
                cell.checkView.isHidden =  false
            } else {
                cell.hasIn.text = nil
                cell.checkView.isHidden = true
            }
        }

        
        return cell
    }

    func preferredContentSize(in controller: QMUIModalPresentationViewController!, keyboardHeight: CGFloat, limitSize: CGSize) -> CGSize {
        let screenWidth = UIScreen.main.bounds.width - 48
        return  CGSize(width: screenWidth, height: 400)
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item:MoneyItem = appMoneyItems[indexPath.row]

        if(self.selectBlock != nil){
            self.selectBlock!(item)
        }
        
    }
    
    
}
